import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class Sort {
    //插入排序(从小到大）
    public static void insertSort(int[] array){
        if (array == null){
            return;
        }
        if (array.length == 1){
            return;
        }
        for (int i = 1; i < array.length; i++){
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--){
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }
                else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array){
        int group = array.length;
        while (group > 1) {
            shell(array, group);
            group = group / 2;
        }
        shell(array, group);
    }

    public static void shell(int[] array, int group){
        for (int i = group; i < array.length; i++){
            int tmp = array[i];
            int j = i - group;
            while (j >= 0){
                if (array[j] > tmp){
                    array[j + group] = array[j];
                }
                else {
                    break;
                }
                j = j - group;
            }
            array[j + group] = tmp;
        }
    }

    //选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++){
            int minIndex = i;
            int j = i + 1;
            for (; j < array.length; j++){
                if (array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }

    public static void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void selectSort2(int[] array){
        int left = 0;
        int right = array.length - 1;
        while (left < right){
            int minIndex = left;
            int maxIndex = left;
            int i = left + 1;
            for (; i <= right; i++){
                if (array[i] < array[minIndex]){
                    minIndex = i;
                }
                if (array[i] > array[maxIndex] ){
                    maxIndex = i;
                }
            }
            swap(array, left, minIndex);
            if (maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array, right , maxIndex);
            left++;
            right--;
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++){
            boolean flag = true;
            for (int j = 0; j < array.length - 1 - i; j++){
                if (array[j] > array[j + 1]){
                    swap(array, j, j + 1);
                    flag = false;
                }
            }
            if (flag){
                return;
            }
        }
    }


    //快速排序
    public static void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }

    public static void quick(int[] array, int start, int end){
        if (start >= end){
            return;
        }
        int index = midThree(array, start, end);
        swap(array, start, index);

        //使用该优化为了减少递归次数
        if (end - start + 1 <= 14){
            insertSort1(array, start, end);
        }

        int pivot = partition(array, start, end);
        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }

    private static void insertSort1(int[] array, int left, int right){
        for (int i = left + 1; i <= right; i++){
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--){
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }
                else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public static int midThree(int[] array, int left, int right){
        int mid = array.length / 2;
        if (array[left] < array[right]){
            if (array[mid] < array[left]){
                return left;
            }
            else if (array[mid] > array[right]){
                return right;
            }
            else {
                return mid;
            }
        }
        else{
            if (array[mid] > array[left]){
                return left;
            }
            else if (array[mid] < array[right]){
                return right;
            }
            else {
                return mid;
            }
        }
    }

    //挖坑法（快排）
    public static int partition(int[] array,int left, int right){
        int tmp = array[left];
        while (left < right){
            //二次判断left < right是为了避免极端情况下的越界，如数组本身就是1 2 3 4
            while (left < right && array[right] >= tmp){
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    //Hoare法
    public static int partition1(int[] array,int left, int right){
        int tmp = array[left];
        int i = left;
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }

    //非递归实现快排
    public static void quickSort1(int[] array){
        Deque<Integer> stack = new LinkedList<>();
        int left = 0;
        int right = array.length - 1;
        int pivot = partition(array, left, right);
        if (pivot > left + 1){
            stack.push(left);
            stack.push(pivot - 1);
        }
        if (pivot < right - 1){
            stack.push(pivot + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array, left, right);
            if (pivot > left + 1){
                stack.push(left);
                stack.push(pivot - 1);
            }
            if (pivot < right - 1){
                stack.push(pivot + 1);
                stack.push(right);
            }
        }
    }

    //归并排序
    public static void merge(int[] array){
        merge1(array, 0, array.length - 1);
    }
    public static void merge1(int[] array, int start, int end){
        if (start >= end){
            return;
        }
        int mid = (end + start) / 2;
        merge1(array, start, mid);
        merge1(array, mid + 1, end);
        mergeFun(array, start, end, mid);
    }

    public static void mergeFun(int[] array, int left, int right, int mid){
        int s1 = left;
        int s2 = mid + 1;
        int[] tmp = new int[right - left + 1];
        int k = 0;
        while (s1 <= mid && s2 <= right){
            if (array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }
            else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid){
            tmp[k++] = array[s1++];
        }
        while (s2 <= right){
            tmp[k++] = array[s2++];
        }
        for (int i = 0 ; i < tmp.length; i++){
            array[i + left] = tmp[i];
        }
    }

    //非递归实现归并排序
    public static void merge1(int[] array){
        int gap = 1;
        while (gap < array.length){
            //i += gap * 2 当前gap组的时候，去排序下一组
            for (int i = 0; i < array.length; i += gap * 2){
                int left = i;
                int mid = left + gap - 1;
                if (mid >= array.length){
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if (right >= array.length){
                    right = array.length - 1;
                }
                mergeFun(array, left, right, mid);
            }
            //当前为2组有序 下次变成4组有序
            gap *= 2;
        }
    }

    //计数排序
    public static void countSort(int[] array){
        //优先得到计数数组的大小
        int min = array[0];
        int max = array[0];
        for (int i = 1; i < array.length; i++){
            if (array[i] < min){
                min = array[i];
            }
            if (array[i] > max){
                max = array[i];
            }
        }
        //得到计数数组长度
        int len = max - min + 1;
        int[] count = new int[len];
        //计数
        for (int i = 0; i < array.length; i++){
            count[array[i] - min]++;
        }
        //遍历计数数组，将数据有序放入array中
        int index = 0;
        for (int i = 0; i < count.length; i++){
            while (count[i] > 0){
                array[index] = i + min;
                index++;
                count[i]--;
            }
        }
    }
















}
